home *** CD-ROM | disk | FTP | other *** search
/ TeX 1995 July / TeX CD-ROM July 1995 (Disc 1)(Walnut Creek)(1995).ISO / biblio / bibtex / contrib / phapalik.bst < prev    next >
Lisp/Scheme  |  1992-07-19  |  63KB  |  1,944 lines

  1. % BibTeX styles for various physics journals (27-Feb-1990).
  2. % This file, physics.btx, is based on the file btxbst.doc.
  3.     % Please notify Charles Karney (Karney@Princeton.EDU)
  4.     % of any bugs, improvements, etc.
  5. % Run this file through cpp to get specific versions.  I have written
  6. % a version of cpp within Emacs.  To access it put
  7. %    (autoload 'cpp "tex$bibtex:cpp"        ; Use cpp for BibTeX styles
  8. %          "C preprocessor"
  9. %          t)
  10. % into your .emacs file.  Read in this file with
  11. %    emacs tex$bibtex:physics.btx
  12. % and run cpp with e.g.,
  13. %    M-x cpp <ret> aip <ret>
  14. % Save the resulting file to tex$latex:aip.bst.
  15. % To accomodate the variations we need (in addition to the definitions below)
  16. %    ATIT_SUPPRESS:    do not include the titles of journal articles
  17. %    NAME_INVERT:    put the initials last
  18. %    NAME_UPPER:    convert names to all caps.
  19. %    MONTH_SUPPRESS:    do not include months in journal articles
  20. %    PAREN_DATE:    dates go in parens in journal articles
  21. %    DATE_FIRST:    dates go before page numbers
  22. %    VOLUME_SPACE:    volume set off with space (not colon)
  23. %    NUM_SUPPRESS:    do not include numbers in journal articles
  24. %    BOLD_VOL:    volume numbers in bold face in journal articles
  25. %    BRACKET_NOTE:    notes in brackets
  26. %    PAGE_START_J:    include only starting page for journal articles
  27. %    PAGE_START_O:    include only starting page for other entries
  28. %    PAGE_ABBREV:    abbreviate page to p. and pages to pp.
  29. %    PHYSICS_JOUR:    include abbreviations for Physics journals
  30. %    COMMA_DELIMIT:    use comma (instead of period) to divide a citation
  31. %    NOTE_SUPPRESS:    suppress the note
  32. %    MAX_NAMES:    max number of authors before using et al (0 = infinity)
  33. %    MIN_NAMES:    number of authors to list with et al.
  34. %    EDIT_VAR:    use "edited by ..." instead of "..., editors"
  35. %    RMP_LABELS:    create RMP-style labels
  36. %    KEY_CITE:    use key as label
  37. %    NAMED_REFS:    use (Smith, 1988) style of citations
  38. %    CSC_NAMES:    names set in caps and small caps
  39. %    JOUR_DEEMPH:    don't emphasize journal name
  40. %    ETAL_EMPH:    emphasize et al.
  41. %    ALT_INCOLL    alternate ordering of fields in incollections
  42. % These are all boolean (0 or 1) except for MAX_NAMES and MIN_NAMES.
  43. % These need to satisfy 0 <= MIN_NAMES <= MAX_NAMES.
  44. % The rule here is that if all these are defined to be zero, we revert to
  45. % the standard styles.  For that reason we initialize them all to 0.
  46. % These are the original macros
  47.     % For American Psychological Association
  48.     % (same as ALPHA but with 0 set and 0 unset)
  49. % This is the start of btxbst.doc
  50. % BibTeX `plain' family
  51.     % version 0.99b for BibTeX versions 0.99a or later, LaTeX version 2.09.
  52.     % Copyright (C) 1985, all rights reserved.
  53.     % Copying of this file is authorized only if either
  54.     % (1) you make absolutely no changes to your copy, including name, or
  55.     % (2) if you do make changes, you name it something other than
  56.     % btxbst.doc, plain.bst, unsrt.bst, alpha.bst, and abbrv.bst.
  57.     % This restriction helps ensure that all standard styles are identical.
  58.     % The file btxbst.doc has the documentation for this style.
  59. % BibTeX `apalike' bibliography style (24-Jan-88 version)
  60. % Adapted from the `alpha' style, version 0.99a; for BibTeX version 0.99a.
  61. % Copyright (C) 1988, all rights reserved.
  62. % Copying of this file is allowed, provided that if you make any changes at all
  63. % you name it something other than `apalike.bst'.
  64. % This restriction helps ensure that all copies are identical.
  65. % Differences between this style and `alpha' are generally heralded by a `%'.
  66. % The file btxbst.doc has the documentation for alpha.bst.
  67. %
  68. % This style should be used with the `apalike' LaTeX style (apalike.sty).
  69. % \cite's come out like "(Jones, 1986)" in the text but there are no labels
  70. % in the bibliography, and something like "(1986)" comes out immediately
  71. % after the author.  Author (and editor) names appear as last name, comma,
  72. % initials.  A `year' field is required for every entry, and so is either
  73. % an author (or in some cases, an editor) field or a key field.
  74. %
  75. % Editorial note:
  76. % Many journals require a style like `apalike', but I strongly, strongly,
  77. % strongly recommend that you not use it if you have a choice---use something
  78. % like `plain' instead.  Mary-Claire van Leunen (A Handbook for Scholars,
  79. % Knopf, 1979) argues convincingly that a style like `plain' encourages better
  80. % writing than one like `apalike'.  Furthermore the strongest arguments for
  81. % using an author-date style like `apalike'---that it's "the most practical"
  82. % (The Chicago Manual of Style, University of Chicago Press, thirteenth
  83. % edition, 1982, pages 400--401)---fall flat on their face with the new
  84. % computer-typesetting technology.  For instance page 401 anachronistically
  85. % states "The chief disadvantage of [a style like `plain'] is that additions
  86. % or deletions cannot be made after the manuscript is typed without changing
  87. % numbers in both text references and list."  LaTeX sidesteps the disadvantage.
  88. %
  89. % History:
  90. %   15-sep-86    (SK,OP)    Original version, by Susan King and Oren Patashnik.
  91. %   10-nov-86    (OP)    Truncated the sort.key$ string to the correct length
  92. %            in bib.sort.order to eliminate error message.
  93. %   24-jan-88    (OP)    Updated for BibTeX version 0.99a, from alpha.bst 0.99a;
  94. %            apalike now sorts by author, then year, then title;
  95. %            THIS `apalike' VERSION DOES NOT WORK WITH BIBTEX 0.98i.
  96. % Please notify Oren Patashnik (PATASHNIK@SCORE.STANFORD.EDU) of any bugs in
  97. % these standard styles or in this documentation file.
  98. %
  99. % This is file btxbxt.doc; it helps document bibliography styles,
  100. % and is also a template file that you can use to make
  101. % several different style files, if you have access to a C preprocessor.
  102. % For example, the standard styles were made by doing something like
  103. %    cpp -P -DPLAIN btxbst.doc plain.txt
  104. %    cpp -P -DUNSRT btxbst.doc unsrt.txt
  105. %    cpp -P -DALPHA btxbst.doc alpha.txt
  106. %    cpp -P -DABBRV btxbst.doc abbrv.txt
  107. % and then renaming after removing unwanted comments and blank lines.
  108. % If you don't have access,
  109. % you can edit this file by hand to imitate the preprocessor,
  110. % with the following explanation of the C preprocessor constructs used here.
  111. %
  112. % The output of the preprocessor is the same as the input, except that certain
  113. % lines will be excluded (and some blank lines will be added).  The sequence
  114. %    #if VAR
  115. %        lines to be included when VAR is not zero
  116. %    #else
  117. %        lines to be included when VAR is zero
  118. %    #endif
  119. % (with the #-signs appearing in column 1) means that one set or the other of
  120. % the lines are to be included depending on the value of VAR.
  121. % The #else part is optional.  Comments can be added after #else and #endif.
  122. % Variables can be set by
  123. %    #define VAR value
  124. % and one can also use #ifdef VAR to see if VAR has any value, and #ifndef
  125. % to see if it has none.
  126. % Another #if form used in this file is #if !VAR, which includes the lines
  127. % after the #if only if VAR is zero.
  128. %
  129. % Convention: Use all uppercase identifiers for these preprocessor variables
  130. % so you can spot them easily
  131. %
  132. % The command line to the preprocessor should define one of PLAIN, UNSRT, ALPHA
  133. % or ABBRV (though PLAIN will be used by default if none is given),
  134. % and the following lines will set various boolean variables to control the
  135. % various lines that are chosen from the rest of the file.
  136. % Each boolean variable should be set true (1) or false (0) in each style.
  137. % Here are the current variables, and their meanings:
  138. %    1:    an alphabetic label is used (if false then a numeric
  139. %                label is used)
  140. %    1:        the entries should be sorted by label (if nonnumeric)
  141. %                and other info, like authors (if false, then
  142. %                entries remain in order of occurrence)
  143. %    0:    the authors, editors, etc., get the full names as
  144. %                given in the bibliography file (if false, the first
  145. %                names become initials)
  146. %    1:    titles of non-"books" (e.g., articles) should be
  147. %                converted to lower-case, except the first letter or
  148. %                first letter after a colon
  149. %                (if false then they appear as in the database)
  150. %    1:    months are spelled out in full (if false, then
  151. %                they're abbreviated)
  152. %    1:    macro journal names are spelled out in full
  153. %                (if false then they are abbreviated, currently
  154. %                as they appear in ACM publications)
  155. %#        define PLAIN 1
  156. %
  157. %   Entry formatting: Similar to that recommended by Mary-Claire van Leunen
  158. %    in "A Handbook for Scholars".  Book-like titles are italicized
  159. %    (emphasized) and non-book titles are converted to sentence
  160. %    capitilization (and not enclosed in quotes).
  161. %    This file outputs a \newblock between major blocks of an entry
  162. %    (the name \newblock is analogous to the names \newline and \newpage)
  163. %    so that the user can obtain an "open" format, which has a line break
  164. %    before each block and lines after the first are indented within blocks,
  165. %    by giving the optional \documentstyle argument `openbib';
  166. %    The default is the "closed" format---blocks runs together.
  167. %
  168. %   Citation alphabetic label format:
  169. %        [Knu73] for single author (or editor or key)
  170. %        [AHU83] (first letters of last names) for multiple authors
  171. %
  172. %   Citation label numberic format:
  173. %        [number]
  174. %
  175. %   Reference list ordering for sorted, alphabetic lables:
  176. %        alphabetical by citation label, then by author(s) or whatever
  177. %        passes for author in the absence of one, then by year,
  178. %        then title
  179. %
  180. %   Reference list ordering for sorted, numeric lables:
  181. %        alphabetical by author(s) or whatever passes
  182. %        for author in the absence of one, then by year, then title
  183. %
  184. %   Reference list ordering for unsorted:
  185. %        by the order cited in the text
  186. %
  187. %   History
  188. %   12/16/84    (HWT)    Original `plain' version, by Howard Trickey.
  189. %   12/23/84    (LL)    Some comments made by Leslie Lamport.
  190. %    2/16/85    (OP)    Changes based on LL's comments, Oren Patashnik.
  191. %    2/17/85    (HWT)    Template file and other standard styles made.
  192. %    3/28/85    (OP)    First release, version 0.98b for BibTeX 0.98f.
  193. %    5/ 9/85    (OP)    Version 0.98c for BibTeX 0.98i:
  194. %            fixed Theoretical Computer Science macro name;
  195. %            fixed the format.vol.num.pages function.
  196. %    1/24/88    (OP)    Version 0.99a for BibTeX 0.99a, main changes:
  197. %            assignment operator (:=) arguments reversed;
  198. %            the preamble$ function outputs the database PREAMBLE;
  199. %            entry.max$ and global.max$ (built-in) variables replace
  200. %            entry.string.max and global.string.max functions;
  201. %            alphabetizing by year then title, not just title;
  202. %            many unnecessary ties removed; \it ==> \em;
  203. %            the `alpha' style uses a superscripted `+' instead of a
  204. %            `*' for unnamed names in constructing the label;
  205. %            the `abbrv' style now uses "Mar." and "Sept.";
  206. %            the functions calc.label and presort now look at just
  207. %            the fields they're supposed to;
  208. %            BOOKLET, MASTERSTHESIS, TECHREPORT use nonbook titles;
  209. %            INBOOK and INCOLLECTION take an optional type (e.g.
  210. %            type = "Section"), overriding the default "chapter";
  211. %            BOOK, INBOOK, INCOLLECTION, and PROCEEDINGS now allow
  212. %            either volume or number, not just volume;
  213. %            INCOLLECTION now allows an edition and series field;
  214. %            PROCEEDINGS and INPROCEEDINGS now use the address field
  215. %            to tell where a conference was held;
  216. %            INPROCEEDINGS and PROCEEDINGS now allow either volume
  217. %            or number, and also a series field;
  218. %            MASTERSTHESIS and PHDTHESIS accept types other than
  219. %            "Master's thesis" and "PhD thesis";
  220. %            UNPUBLISHED now outputs, in one block, note then date;
  221. %            MANUAL now prints out the organization in
  222. %            the first block if the author field is empty;
  223. %            MISC can't be empty---it requires some optional field.
  224. %    3/23/88    (OP)    Version 0.99b for BibTeX 0.99c---changed the three
  225. %            erroneous occurrences of `cite ' to `cite$ '; this
  226. %            change didn't affect the four standard styles, so the
  227. %            0.99a versions of those styles are still current.
  228. %
  229. % The ENTRY declaration
  230. %   Like Scribe's (according to pages 231-2 of the April '84 edition),
  231. %   but no fullauthor or editors fields because BibTeX does name handling.
  232. %   The annote field is commented out here because this family doesn't
  233. %   include an annotated bibliography style.  And in addition to the fields
  234. %   listed here, BibTeX has a built-in crossref field, explained later.
  235. ENTRY
  236. % Fields:
  237.   { address
  238. %        Usually the address of a publisher or other type of organization.
  239. %        Put information in this field only if it helps the reader find the
  240. %        thing---for example you should omit the address of a major
  241. %        publisher entirely.  For a PROCEEDINGS or an INPROCEEDINGS,
  242. %        however, it's the address of the conference; for those two entry
  243. %        types, include the publisher's or organization's address, if
  244. %        necessary, in the publisher or organization field.
  245. %    annote
  246. %        Long annotation---for annotated bibliographies (begins sentence).
  247.     author
  248. %        Name(s) of author(s), in BibTeX name format.
  249.     booktitle
  250. %        Book title when the thing being referenced isn't the whole book.
  251. %        For book entries, the title field should be used instead.
  252.     chapter
  253. %        Chapter (or section or whatever) number.
  254.     edition
  255. %        Edition of a book---should be an ordinal (e.g., "Second").
  256.     editor
  257. %        Name(s) of editor(s), in BibTeX name format.
  258. %        If there is also an author field, then the editor field should be
  259. %        for the book or collection that the work appears in.
  260.     howpublished
  261. %         How something strange has been published (begins sentence).
  262.     institution
  263. %        Sponsoring institution of a technical report.
  264.     journal
  265. %        Journal name (macros are provided for many).
  266.     key
  267. %        Alphabetizing, labeling, and cross-referencing key
  268. %        (needed when an entry has no author or editor).
  269. %    month        not used in apalike
  270. %        Month (macros are provided).
  271.     note
  272. %        To help the reader find a reference (begins sentence).
  273.     number
  274. %        Number of a journal or technical report, or of a work in a series.
  275.     organization
  276. %        Organization sponsoring a conference (or publishing a manual); if
  277. %        the editor (or author) is empty, and if the organization produces
  278. %        an awkward label or cross reference, you should put appropriately
  279. %        condensed organization information in the key field as well.
  280.     pages
  281. %        Page number or numbers (use `--' to separate a range, use `+'
  282. %        to indicate pages following that don't form a simple range).
  283.     publisher
  284. %        Publisher name.
  285.     school
  286. %        School name (for theses).
  287.     series
  288. %        The name of a series or set of books.
  289. %        An individual book will will also have it's own title.
  290.     title
  291. %        The title of the thing you're referred to.
  292.     type
  293. %        Type of a Techreport (e.g., "Research Note") to be used instead of
  294. %        the default "Technical Report"; or, similarly, the type of a
  295. %        thesis; or of a part of a book.
  296.     volume
  297. %        The volume number of a journal or multivolume work.
  298.     year
  299. %        The year should contain only numerals (technically, it should end
  300. %        with four numerals, after purification; doesn't a begin sentence).
  301.   }
  302. % There are no integer entry variables
  303.   {}
  304. % These string entry variables are used to form the citation label.
  305. % In a storage pinch, sort.label can be easily computed on the fly.
  306.   { label extra.label sort.label }
  307. % Each entry function starts by calling output.bibitem, to write the
  308. % \bibitem and its arguments to the .BBL file.  Then the various fields
  309. % are formatted and printed by output or output.check.  Those functions
  310. % handle the writing of separators (commas, periods, \newblock's),
  311. % taking care not to do so when they are passed a null string.
  312. % Finally, fin.entry is called to add the final period and finish the
  313. % entry.
  314. %
  315. % A bibliographic reference is formatted into a number of `blocks':
  316. % in the open format, a block begins on a new line and subsequent
  317. % lines of the block are indented.  A block may contain more than
  318. % one sentence (well, not a grammatical sentence, but something to
  319. % be ended with a sentence ending period).  The entry functions should
  320. % call new.block whenever a block other than the first is about to be
  321. % started.  They should call new.sentence whenever a new sentence is
  322. % to be started.  The output functions will ensure that if two
  323. % new.sentence's occur without any non-null string being output between
  324. % them then there won't be two periods output.  Similarly for two
  325. % successive new.block's.
  326. %
  327. % The output routines don't write their argument immediately.
  328. % Instead, by convention, that argument is saved on the stack to be
  329. % output next time (when we'll know what separator needs to come
  330. % after it).  Meanwhile, the output routine has to pop the pending
  331. % output off the stack, append any needed separator, and write it.
  332. %
  333. % To tell which separator is needed, we maintain an output.state.
  334. % It will be one of these values:
  335. %    before.all        just after the \bibitem
  336. %    mid.sentence        in the middle of a sentence: comma needed
  337. %                    if more sentence is output
  338. %    after.sentence        just after a sentence: period needed
  339. %    after.block        just after a block (and sentence):
  340. %                    period and \newblock needed.
  341. % Note: These styles don't use after.sentence
  342. %
  343. % VAR: output.state : INTEGER        -- state variable for output
  344. %
  345. % The output.nonnull function saves its argument (assumed to be nonnull)
  346. % on the stack, and writes the old saved value followed by any needed
  347. % separator.  The ordering of the tests is decreasing frequency of
  348. % occurrence.
  349. %
  350. % output.nonnull(s) ==
  351. %  BEGIN
  352. %    s := argument on stack
  353. %    if output.state = mid.sentence then
  354. %        write$(pop() * ", ")
  355. %          -- "pop" isn't a function: just use stack top
  356. %    else
  357. %        if output.state = after.block then
  358. %        write$(add.period$(pop()))
  359. %        newline$
  360. %        write$("\newblock ")
  361. %        else
  362. %        if output.state = before.all then
  363. %            write$(pop())
  364. %        else        -- output.state should be after.sentence
  365. %            write$(add.period$(pop()) * " ")
  366. %        fi
  367. %        fi
  368. %        output.state := mid.sentence
  369. %    fi
  370. %    push s on stack
  371. %  END
  372. %
  373. % The output function calls output.nonnull if its argument is non-empty;
  374. % its argument may be a missing field (thus, not necessarily a string)
  375. %
  376. % output(s) ==
  377. %  BEGIN
  378. %    if not empty$(s) then output.nonnull(s)
  379. %    fi
  380. %  END
  381. %
  382. % The output.check function is the same as the output function except that, if
  383. % necessary, output.check warns the user that the t field shouldn't be empty
  384. % (this is because it probably won't be a good reference without the field;
  385. % the entry functions try to make the formatting look reasonable even when
  386. % such fields are empty).
  387. %
  388. % output.check(s,t) ==
  389. %  BEGIN
  390. %    if empty$(s) then
  391. %        warning$("empty " * t * " in " * cite$)
  392. %    else output.nonnull(s)
  393. %    fi
  394. %  END
  395. %
  396. % The output.bibitem function writes the \bibitem for the current entry
  397. % (the label should already have been set up), and sets up the separator
  398. % state for the output functions.  And, it leaves a string on the stack
  399. % as per the output convention.
  400. %
  401. % output.bibitem ==
  402. %  BEGIN
  403. %    newline$
  404. %    write$("\bibitem[")    % for alphabetic labels,
  405. %    write$(label)        % these three lines
  406. %    write$("]{")        % are used
  407. %    write$("\bibitem{")        % this line for numeric labels
  408. %    write$(cite$)
  409. %    write$("}")
  410. %    push "" on stack
  411. %    output.state := before.all
  412. %  END
  413. %
  414. % The fin.entry function finishes off an entry by adding a period to the
  415. % string remaining on the stack.  If the state is still before.all
  416. % then nothing was produced for this entry, so the result will look bad,
  417. % but the user deserves it. (We don't omit the whole entry because the
  418. % entry was cited, and a bibitem is needed to define the citation label.)
  419. %
  420. % fin.entry ==
  421. %  BEGIN
  422. %    write$(add.period$(pop()))
  423. %    newline$
  424. %  END
  425. %
  426. % The new.block function prepares for a new block to be output, and
  427. % new.sentence prepares for a new sentence.
  428. %
  429. % new.block ==
  430. %  BEGIN
  431. %    if output.state <> before.all then
  432. %        output.state := after.block
  433. %    fi
  434. %  END
  435. %
  436. % new.sentence ==
  437. %  BEGIN
  438. %    if output.state <> after.block then
  439. %        if output.state <> before.all then
  440. %        output.state :=  after.sentence
  441. %        fi
  442. %    fi
  443. %  END
  444. %
  445. INTEGERS { output.state before.all mid.sentence after.sentence after.block }
  446. FUNCTION {init.state.consts}
  447. { #0 'before.all :=
  448.   #1 'mid.sentence :=
  449.   #2 'after.sentence :=
  450.   #3 'after.block :=
  451. }
  452. % the variables s and t are temporary string holders
  453. STRINGS { s t }
  454. FUNCTION {output.nonnull}
  455. { 's :=
  456.   output.state mid.sentence =
  457.     { ", " * write$ }
  458.     { output.state after.block =
  459.     { add.period$ write$
  460.       newline$
  461.       "\newblock " write$
  462.     }
  463.     { output.state before.all =
  464.         'write$
  465.         { add.period$ " " * write$ }
  466.       if$
  467.     }
  468.       if$
  469.       mid.sentence 'output.state :=
  470.     }
  471.   if$
  472.   s
  473. }
  474. FUNCTION {output}
  475. { duplicate$ empty$
  476.     'pop$
  477.     'output.nonnull
  478.   if$
  479. }
  480. FUNCTION {output.check}
  481. { 't :=
  482.   duplicate$ empty$
  483.     { pop$ "empty " t * " in " * cite$ * warning$ }
  484.     'output.nonnull
  485.   if$
  486. }
  487. %                    apalike needs this function because
  488. %                    the year has special punctuation;
  489. %                    apalike ignores the month
  490. FUNCTION {output.year.check}
  491. { year empty$
  492.     { "empty year in " cite$ * warning$ }
  493.     { write$
  494.       " (" year * extra.label * ")" *
  495.       mid.sentence 'output.state :=
  496.     }
  497.   if$
  498. }
  499. FUNCTION {output.bibitem}
  500. { newline$
  501.   "\bibitem[" write$
  502.   label write$
  503.   "]{" write$
  504.   cite$ write$
  505.   "}" write$
  506.   newline$
  507.   ""
  508.   before.all 'output.state :=
  509. }
  510. % This function finishes all entries.
  511. FUNCTION {fin.entry}
  512. { add.period$
  513.   write$
  514.   newline$
  515. }
  516. FUNCTION {new.block}
  517. { output.state before.all =
  518.     'skip$
  519.     { after.block 'output.state := }
  520.   if$
  521. }
  522. FUNCTION {new.sentence}
  523. { output.state after.block =
  524.     'skip$
  525.     { output.state before.all =
  526.     'skip$
  527.     { after.sentence 'output.state := }
  528.       if$
  529.     }
  530.   if$
  531. }
  532. % These three functions pop one or two (integer) arguments from the stack
  533. % and push a single one, either 0 or 1.
  534. % The 'skip$ in the `and' and `or' functions are used because
  535. % the corresponding if$ would be idempotent
  536. FUNCTION {not}
  537. {   { #0 }
  538.     { #1 }
  539.   if$
  540. }
  541. FUNCTION {and}
  542. {   'skip$
  543.     { pop$ #0 }
  544.   if$
  545. }
  546. FUNCTION {or}
  547. {   { pop$ #1 }
  548.     'skip$
  549.   if$
  550. }
  551. % Sometimes we begin a new block only if the block will be big enough.  The
  552. % new.block.checka function issues a new.block if its argument is nonempty;
  553. % new.block.checkb does the same if either of its TWO arguments is nonempty.
  554. FUNCTION {new.block.checkb}
  555. { empty$
  556.   swap$ empty$
  557.   and
  558.     'skip$
  559.     'new.block
  560.   if$
  561. }
  562. % Here are some functions for formatting chunks of an entry.
  563. % By convention they either produce a string that can be followed by
  564. % a comma or period (using add.period$, so it is OK to end in a period),
  565. % or they produce the null string.
  566. %
  567. % A useful utility is the field.or.null function, which checks if the
  568. % argument is the result of pushing a `missing' field (one for which no
  569. % assignment was made when the current entry was read in from the database)
  570. % or the result of pushing a string having no non-white-space characters.
  571. % It returns the null string if so, otherwise it returns the field string.
  572. % Its main (but not only) purpose is to guarantee that what's left on the
  573. % stack is a string rather than a missing field.
  574. %
  575. % field.or.null(s) ==
  576. %  BEGIN
  577. %    if empty$(s) then return ""
  578. %    else return s
  579. %  END
  580. %
  581. % Another helper function is emphasize, which returns the argument emphazised,
  582. % if that is non-empty, otherwise it returns the null string.  Italic
  583. % corrections aren't used, so this function should be used when punctation
  584. % will follow the result.
  585. %
  586. % emphasize(s) ==
  587. %  BEGIN
  588. %    if empty$(s) then return ""
  589. %    else return "{\em " * s * "}"
  590. %
  591. % The format.names function formats the argument (which should be in
  592. % BibTeX name format) into "First Von Last, Junior", separated by commas
  593. % and with an "and" before the last (but ending with "et~al." if the last
  594. % of multiple authors is "others").  This function's argument should always
  595. % contain at least one name.
  596. %
  597. % VAR: nameptr, namesleft, numnames: INTEGER
  598. % pseudoVAR: nameresult: STRING        (it's what's accumulated on the stack)
  599. %
  600. % format.names(s) ==
  601. %  BEGIN
  602. %    nameptr := 1
  603. %    numnames := num.names$(s)
  604. %    namesleft := numnames
  605. %    while namesleft > 0
  606. %      do
  607. %                % for full names:
  608. %        t := format.name$(s, nameptr, "{ff~}{vv~}{ll}{, jj}")
  609. %                % for abbreviated first names:
  610. %        t := format.name$(s, nameptr, "{f.~}{vv~}{ll}{, jj}")
  611. %        if nameptr > 1 then
  612. %        if namesleft > 1 then nameresult := nameresult * ", " * t
  613. %        else if numnames > 2
  614. %               then nameresult := nameresult * ","
  615. %             fi
  616. %             if t = "others"
  617. %               then nameresult := nameresult * " et~al."
  618. %               else nameresult := nameresult * " and " * t
  619. %             fi
  620. %        fi
  621. %        else nameresult := t
  622. %        fi
  623. %        nameptr := nameptr + 1
  624. %        namesleft := namesleft - 1
  625. %      od
  626. %    return nameresult
  627. %  END
  628. %
  629. % The format.authors function returns the result of format.names(author)
  630. % if the author is present, or else it returns the null string
  631. %
  632. % format.authors ==
  633. %  BEGIN
  634. %    if empty$(author) then return ""
  635. %    else return format.names(author)
  636. %    fi
  637. %  END
  638. %
  639. % Format.editors is like format.authors, but it uses the editor field,
  640. % and appends ", editor" or ", editors"
  641. %
  642. % format.editors ==
  643. %  BEGIN
  644. %    if empty$(editor) then return ""
  645. %    else
  646. %        if num.names$(editor) > 1 then
  647. %        return format.names(editor) * ", editors"
  648. %        else
  649. %        return format.names(editor) * ", editor"
  650. %        fi
  651. %    fi
  652. %  END
  653. %
  654. % Other formatting functions are similar, so no "comment version" will be
  655. % given for them.
  656. %
  657. % The `pop$' in this function gets rid of the duplicate `empty' value and
  658. % the `skip$' returns the duplicate field value
  659. FUNCTION {field.or.null}
  660. { duplicate$ empty$
  661.     { pop$ "" }
  662.     'skip$
  663.   if$
  664. }
  665. FUNCTION {emphasize}
  666. { duplicate$ empty$
  667.     { pop$ "" }
  668.     { "{\em " swap$ * "}" * }
  669.   if$
  670. }
  671. INTEGERS { nameptr namesleft numnames }
  672. FUNCTION {format.names}
  673. { 's :=
  674.   #1 'nameptr :=
  675.   s num.names$ 'numnames :=
  676.   numnames 'namesleft :=
  677.     { namesleft #0 > }
  678.     { s nameptr "{vv~}{ll}{, jj}{, f.}" format.name$ 't :=   % last name first
  679.       nameptr #1 >
  680.     { namesleft #1 >
  681.         { ", " * t * }
  682.         { numnames #2 >
  683.         { "," * }
  684.         'skip$
  685.           if$
  686.           t "others" =
  687.         { " et~al." * }
  688.         { " and " * t * }
  689.           if$
  690.         }
  691.       if$
  692.     }
  693.     't
  694.       if$
  695.       nameptr #1 + 'nameptr :=
  696.       namesleft #1 - 'namesleft :=
  697.     }
  698.   while$
  699. }
  700. FUNCTION {format.authors}
  701. { author empty$
  702.     { "" }
  703.     { author format.names }
  704.   if$
  705. }
  706. FUNCTION {format.key}            % this function is just for apalike
  707. { empty$
  708.     { key field.or.null }
  709.     { "" }
  710.   if$
  711. }
  712. FUNCTION {format.editors}
  713. { editor empty$
  714.     { "" }
  715.     { editor format.names
  716.       editor num.names$ #1 >
  717.     { ", editors" * }
  718.     { ", editor" * }
  719.       if$
  720.     }
  721.   if$
  722. }
  723. % The format.title function is used for non-book-like titles.
  724. % For most styles we convert to lowercase (except for the very first letter,
  725. % and except for the first one after a colon (followed by whitespace)),
  726. % and hope the user has brace-surrounded words that need to stay capitilized;
  727. % for some styles, however, we leave it as it is in the database.
  728. FUNCTION {format.title}
  729. { title empty$
  730.     { "" }
  731.     { title "t" change.case$ }
  732.   if$
  733. }
  734. % By default, BibTeX sets the global integer variable global.max$ to the BibTeX
  735. % constant glob_str_size, the maximum length of a global string variable.
  736. % Analogously, BibTeX sets the global integer variable entry.max$ to
  737. % ent_str_size, the maximum length of an entry string variable.
  738. % The style designer may change these if necessary (but this is unlikely)
  739. % The n.dashify function makes each single `-' in a string a double `--'
  740. % if it's not already
  741. %
  742. % pseudoVAR: pageresult: STRING        (it's what's accumulated on the stack)
  743. %
  744. % n.dashify(s) ==
  745. %  BEGIN
  746. %    t := s
  747. %    pageresult := ""
  748. %    while (not empty$(t))
  749. %      do
  750. %        if (first character of t = "-")
  751. %          then
  752. %        if (next character isn't)
  753. %          then
  754. %            pageresult := pageresult * "--"
  755. %            t := t with the "-" removed
  756. %          else
  757. %            while (first character of t = "-")
  758. %              do
  759. %            pageresult := pageresult * "-"
  760. %            t := t with the "-" removed
  761. %              od
  762. %        fi
  763. %          else
  764. %        pageresult := pageresult * the first character
  765. %        t := t with the first character removed
  766. %        fi
  767. %      od
  768. %    return pageresult
  769. %  END
  770. FUNCTION {n.dashify}
  771. { 't :=
  772.   ""
  773.     { t empty$ not }
  774.     { t #1 #1 substring$ "-" =
  775.     { t #1 #2 substring$ "--" = not
  776.         { "--" *
  777.           t #2 global.max$ substring$ 't :=
  778.         }
  779.         {   { t #1 #1 substring$ "-" = }
  780.         { "-" *
  781.           t #2 global.max$ substring$ 't :=
  782.         }
  783.           while$
  784.         }
  785.       if$
  786.     }
  787.     { t #1 #1 substring$ *
  788.       t #2 global.max$ substring$ 't :=
  789.     }
  790.       if$
  791.     }
  792.   while$
  793. }
  794. % The format.btitle is for formatting the title field when it is a book-like
  795. % entry---the style used here keeps it in uppers-and-lowers and emphasizes it.
  796. FUNCTION {format.btitle}
  797. { title emphasize
  798. }
  799. % For several functions we'll need to connect two strings with a
  800. % tie (~) if the second one isn't very long (fewer than 3 characters).
  801. % The tie.or.space.connect function does that.  It concatenates the two
  802. % strings on top of the stack, along with either a tie or space between
  803. % them, and puts this concatenation back onto the stack:
  804. %
  805. % tie.or.space.connect(str1,str2) ==
  806. %    BEGIN
  807. %    if text.length$(str2) < 3
  808. %      then return the concatenation of str1, "~", and str2
  809. %      else return the concatenation of str1, " ", and str2
  810. %    END
  811. FUNCTION {tie.or.space.connect}
  812. { duplicate$ text.length$ #3 <
  813.     { "~" }
  814.     { " " }
  815.   if$
  816.   swap$ * *
  817. }
  818. % The either.or.check function complains if both fields or an either-or pair
  819. % are nonempty.
  820. %
  821. % either.or.check(t,s) ==
  822. %  BEGIN
  823. %    if empty$(s) then
  824. %        warning$(can't use both " * t * " fields in " * cite$)
  825. %    fi
  826. %  END
  827. FUNCTION {either.or.check}
  828. { empty$
  829.     'pop$
  830.     { "can't use both " swap$ * " fields in " * cite$ * warning$ }
  831.   if$
  832. }
  833. % The format.bvolume function is for formatting the volume and perhaps
  834. % series name of a multivolume work.  If both a volume and a series field
  835. % are there, we assume the series field is the title of the whole multivolume
  836. % work (the title field should be the title of the thing being referred to),
  837. % and we add an "of <series>".  This function is called in mid-sentence.
  838. FUNCTION {format.bvolume}
  839. { volume empty$
  840.     { "" }
  841.     { "volume" volume tie.or.space.connect
  842.       series empty$
  843.     'skip$
  844.     { " of " * series emphasize * }
  845.       if$
  846.       "volume and number" number either.or.check
  847.     }
  848.   if$
  849. }
  850. % The format.number.series function is for formatting the series name
  851. % and perhaps number of a work in a series.  This function is similar to
  852. % format.bvolume, although for this one the series must exist (and the
  853. % volume must not exist).  If the number field is empty we output either
  854. % the series field unchanged if it exists or else the null string.
  855. % If both the number and series fields are there we assume the series field
  856. % gives the name of the whole series (the title field should be the title
  857. % of the work being one referred to), and we add an "in <series>".
  858. % We capitilize Number when this function is used at the beginning of a block.
  859. FUNCTION {format.number.series}
  860. { volume empty$
  861.     { number empty$
  862.     { series field.or.null }
  863.     { output.state mid.sentence =
  864.         { "number" }
  865.         { "Number" }
  866.       if$
  867.       number tie.or.space.connect
  868.       series empty$
  869.         { "there's a number but no series in " cite$ * warning$ }
  870.         { " in " * series * }
  871.       if$
  872.     }
  873.       if$
  874.     }
  875.     { "" }
  876.   if$
  877. }
  878. % The format.edition function appends " edition" to the edition, if present.
  879. % We lowercase the edition (it should be something like "Third"), because
  880. % this doesn't start a sentence.
  881. FUNCTION {format.edition}
  882. { edition empty$
  883.     { "" }
  884.     { output.state mid.sentence =
  885.     { edition "l" change.case$ " edition" * }
  886.     { edition "t" change.case$ " edition" * }
  887.       if$
  888.     }
  889.   if$
  890. }
  891. % The format.pages function is used for formatting a page range in a book
  892. % (and in rare circumstances, an article).
  893. %
  894. % The multi.page.check function examines the page field for a "-" or "," or "+"
  895. % so that format.pages can use "page" instead of "pages" if none exists.
  896. % Note: global.max$ here means "take the rest of the string"
  897. %
  898. % VAR: multiresult: INTEGER    (actually, a boolean)
  899. %
  900. % multi.page.check(s) ==
  901. %  BEGIN
  902. %    t := s
  903. %    multiresult := false
  904. %    while ((not multiresult) and (not empty$(t)))
  905. %      do
  906. %        if (first character of t = "-" or "," or "+")
  907. %          then multiresult := true
  908. %          else t := t with the first character removed
  909. %        fi
  910. %      od
  911. %    return multiresult
  912. %  END
  913. INTEGERS { multiresult }
  914. FUNCTION {multi.page.check}
  915. { 't :=
  916.   #0 'multiresult :=
  917.     { multiresult not
  918.       t empty$ not
  919.       and
  920.     }
  921.     { t #1 #1 substring$
  922.       duplicate$ "-" =
  923.       swap$ duplicate$ "," =
  924.       swap$ "+" =
  925.       or or
  926.     { #1 'multiresult := }
  927.     { t #2 global.max$ substring$ 't := }
  928.       if$
  929.     }
  930.   while$
  931.   multiresult
  932. }
  933. % This function doesn't begin a sentence so "pages" isn't capitalized.
  934. % Other functions that use this should keep that in mind.
  935. FUNCTION {format.pages}
  936. { pages empty$
  937.     { "" }
  938.     { pages multi.page.check
  939.     { "pages" pages n.dashify tie.or.space.connect }
  940.     { "page" pages tie.or.space.connect }
  941.       if$
  942.     }
  943.   if$
  944. }
  945. % The format.vol.num.pages function is for the volume, number, and page range
  946. % of a journal article.  We use the format:  vol(number):pages, with some
  947. % variations for empty fields.  This doesn't begin a sentence.
  948. FUNCTION {format.vol.num.pages}
  949. { volume field.or.null
  950.   number empty$
  951.     'skip$
  952.     { "(" number * ")" * *
  953.       volume empty$
  954.     { "there's a number but no volume in " cite$ * warning$ }
  955.     'skip$
  956.       if$
  957.     }
  958.   if$
  959.   pages empty$
  960.     'skip$
  961.     { duplicate$ empty$
  962.     { pop$ format.pages }
  963.     { ":" * pages n.dashify * }
  964.       if$
  965.     }
  966.   if$
  967. }
  968. % The format.chapter.pages, if the chapter is present, puts whatever is in the
  969. % type field (or else "chapter" if type is empty) in front of a chapter number.
  970. % It then appends the pages, if present.  This doesn't begin a sentence.
  971. FUNCTION {format.chapter.pages}
  972. { chapter empty$
  973.     'format.pages
  974.     { type empty$
  975.     { "chapter" }
  976.     { type "l" change.case$ }
  977.       if$
  978.       chapter tie.or.space.connect
  979.       pages empty$
  980.     'skip$
  981.     { ", " * format.pages * }
  982.       if$
  983.     }
  984.   if$
  985. }
  986. % The format.in.ed.booktitle function is used for starting out a sentence
  987. % that begins "In <booktitle>", putting an editor before the title if one
  988. % exists.
  989. FUNCTION {format.in.ed.booktitle}
  990. { booktitle empty$
  991.     { "" }
  992.     { editor empty$
  993.     { "In " booktitle emphasize * }
  994.     { "In " format.editors * ", " * booktitle emphasize * }
  995.       if$
  996.     }
  997.   if$
  998. }
  999. % The function format.thesis.type returns either the (case-changed) type field,
  1000. % if it is defined, or else the default string already on the stack
  1001. % (like "Master's thesis" or "PhD thesis").
  1002. FUNCTION {format.thesis.type}
  1003. { type empty$
  1004.     'skip$
  1005.     { pop$
  1006.       type "t" change.case$
  1007.     }
  1008.   if$
  1009. }
  1010. % The function format.tr.number makes a string starting with "Technical Report"
  1011. % (or type, if that field is defined), followed by the number if there is one;
  1012. % it returns the starting part (with a case change) even if there is no number.
  1013. % This is used at the beginning of a sentence.
  1014. FUNCTION {format.tr.number}
  1015. { type empty$
  1016.     { "Technical Report" }
  1017.     'type
  1018.   if$
  1019.   number empty$
  1020.     { "t" change.case$ }
  1021.     { number tie.or.space.connect }
  1022.   if$
  1023. }
  1024. % Now come the cross-referencing functions (these are invoked because
  1025. % one entry in the database file(s) cross-references another, by giving
  1026. % the other entry's database key in a `crossref' field).  This feature
  1027. % allows one or more titled things that are part of a larger titled
  1028. % thing to cross-reference the larger thing.  These styles allow for
  1029. % five posibilities: (1) an ARTICLE may cross-reference an ARTICLE;
  1030. % (2) a BOOK, (3) INBOOK, or (4) INCOLLECTION may cross-reference a BOOK;
  1031. % or (5) an INPROCEEDINGS may cross-reference a PROCEEDINGS.
  1032. % Each of these is explained in more detail later.
  1033. %
  1034. % An ARTICLE entry type may cross reference another ARTICLE (this is
  1035. % intended for when an entire journal is devoted to a single topic---
  1036. % but since there is no JOURNAL entry type, the journal, too, should be
  1037. % classified as an ARTICLE but without the author and title fields).
  1038. % This will result in two warning messages for the journal's entry
  1039. % if it's included in the reference list, but such is life.
  1040. %
  1041. % format.article.crossref ==
  1042. %  BEGIN
  1043. %    if empty$(key) then
  1044. %        if empty$(journal) then
  1045. %        warning$("need key or journal for " * cite$ *
  1046. %                        " to crossref " * crossref)
  1047. %        return(" \cite{" * crossref * "}")
  1048. %        else
  1049. %        return("In " * emphazise.correct (journal) *
  1050. %                        " \cite{" * crossref * "}")
  1051. %        fi
  1052. %    else
  1053. %        return("In " * key * " \cite{" * crossref * "}")
  1054. %    fi
  1055. %  END
  1056. %
  1057. % The other cross-referencing functions are similar, so no "comment version"
  1058. % will be given for them.
  1059. FUNCTION {format.article.crossref}
  1060. { "In"                            % this is for apalike
  1061.   " \cite{" * crossref * "}" *
  1062. }
  1063. % A BOOK (or INBOOK) entry type (assumed to be for a single volume in a
  1064. % multivolume work) may cross reference another BOOK (the entire multivolume).
  1065. % Usually there will be an editor, in which case we use that to construct the
  1066. % cross reference; otherwise we use a nonempty key field or else the series
  1067. % field (since the series gives the title of the multivolume work).
  1068. FUNCTION {format.book.crossref}
  1069. { volume empty$
  1070.     { "empty volume in " cite$ * "'s crossref of " * crossref * warning$
  1071.       "In "
  1072.     }
  1073.     { "Volume" volume tie.or.space.connect
  1074.       " of " *
  1075.     }
  1076.   if$
  1077.   "\cite{" * crossref * "}" *                % this is for apalike
  1078. }
  1079. % An INCOLLECTION entry type may cross reference a BOOK (assumed to be the
  1080. % collection), or an INPROCEEDINGS may cross reference a PROCEEDINGS.
  1081. % Often there will be an editor, in which case we use that to construct
  1082. % the cross reference; otherwise we use a nonempty key field or else
  1083. % the booktitle field (which gives the cross-referenced work's title).
  1084. FUNCTION {format.incoll.inproc.crossref}
  1085. { "In"                            % this is for apalike
  1086.   " \cite{" * crossref * "}" *
  1087. }
  1088. % Now we define the type functions for all entry types that may appear
  1089. % in the .BIB file---e.g., functions like `article' and `book'.  These
  1090. % are the routines that actually generate the .BBL-file output for
  1091. % the entry.  These must all precede the READ command.  In addition, the
  1092. % style designer should have a function `default.type' for unknown types.
  1093. % Note: The fields (within each list) are listed in order of appearance,
  1094. % except as described for an `inbook' or a `proceedings'.
  1095. %
  1096. % The article function is for an article in a journal.  An article may
  1097. % CROSSREF another article.
  1098. %    Required fields: author, title, journal, year
  1099. %    Optional fields: volume, number, pages, month, note
  1100. %
  1101. % article ==
  1102. %  BEGIN
  1103. %    output.bibitem
  1104. %    output.check(format.authors,"author")
  1105. %    new.block
  1106. %    output.check(format.title,"title")
  1107. %    new.block
  1108. %    if missing$(crossref) then
  1109. %        output.check(emphasize(journal),"journal")
  1110. %        output(format.vol.num.pages)
  1111. %        output.check(format.date,"year")
  1112. %       else
  1113. %        output.nonnull(format.article.crossref)
  1114. %        output(format.pages)
  1115. %    fi
  1116. %    new.block
  1117. %    output(note)
  1118. %    fin.entry
  1119. %  END
  1120. %
  1121. % The book function is for a whole book.  A book may CROSSREF another book.
  1122. %    Required fields: author or editor, title, publisher, year
  1123. %    Optional fields: volume or number, series, address, edition, month,
  1124. %            note
  1125. %
  1126. % book ==
  1127. %  BEGIN
  1128. %    if empty$(author) then output.check(format.editors,"author and editor")
  1129. %    else    output.check(format.authors,"author")
  1130. %        if missing$(crossref) then
  1131. %            either.or.check("author and editor",editor)
  1132. %        fi
  1133. %    fi
  1134. %    new.block
  1135. %    output.check(format.btitle,"title")
  1136. %    if missing$(crossref) then
  1137. %        output(format.bvolume)
  1138. %        new.block
  1139. %        output(format.number.series)
  1140. %        new.sentence
  1141. %        output.check(publisher,"publisher")
  1142. %        output(address)
  1143. %       else
  1144. %        new.block
  1145. %        output.nonnull(format.book.crossref)
  1146. %    fi
  1147. %    output(format.edition)
  1148. %    output.check(format.date,"year")
  1149. %    new.block
  1150. %    output(note)
  1151. %    fin.entry
  1152. %  END
  1153. %
  1154. % The other entry functions are all quite similar, so no "comment version"
  1155. % will be given for them.
  1156. FUNCTION {article}
  1157. { output.bibitem
  1158.   format.authors "author" output.check
  1159.   author format.key output                % special for
  1160.   output.year.check                    % apalike
  1161.   new.block
  1162.   format.title "title" output.check
  1163.   new.block
  1164.   crossref missing$
  1165.     { journal emphasize "journal" output.check
  1166.       format.vol.num.pages output
  1167.     }
  1168.     { format.article.crossref output.nonnull
  1169.       format.pages output
  1170.     }
  1171.   if$
  1172.   new.block
  1173.   note output
  1174.   fin.entry
  1175. }
  1176. FUNCTION {book}
  1177. { output.bibitem
  1178.   author empty$
  1179.     { format.editors "author and editor" output.check
  1180.       editor format.key output
  1181.     }
  1182.     { format.authors output.nonnull
  1183.       crossref missing$
  1184.     { "author and editor" editor either.or.check }
  1185.     'skip$
  1186.       if$
  1187.     }
  1188.   if$
  1189.   output.year.check                % special for apalike
  1190.   new.block
  1191.   format.btitle "title" output.check
  1192.   crossref missing$
  1193.     { format.bvolume output
  1194.       new.block
  1195.       format.number.series output
  1196.       new.sentence
  1197.       publisher "publisher" output.check
  1198.       address output
  1199.     }
  1200.     { new.block
  1201.       format.book.crossref output.nonnull
  1202.     }
  1203.   if$
  1204.   format.edition output
  1205.   new.block
  1206.   note output
  1207.   fin.entry
  1208. }
  1209. % A booklet is a bound thing without a publisher or sponsoring institution.
  1210. %    Required: title
  1211. %    Optional: author, howpublished, address, month, year, note
  1212. FUNCTION {booklet}
  1213. { output.bibitem
  1214.   format.authors output
  1215.   author format.key output                % special for
  1216.   output.year.check                    % apalike
  1217.   new.block
  1218.   format.title "title" output.check
  1219.   new.block
  1220.   howpublished output
  1221.   address output
  1222.   new.block
  1223.   note output
  1224.   fin.entry
  1225. }
  1226. % For the conference entry type, see inproceedings.
  1227. % An inbook is a piece of a book: either a chapter and/or a page range.
  1228. % It may CROSSREF a book.  If there's no volume field, the type field
  1229. % will come before number and series.
  1230. %    Required: author or editor, title, chapter and/or pages, publisher,year
  1231. %    Optional: volume or number, series, type, address, edition, month, note
  1232. FUNCTION {inbook}
  1233. { output.bibitem
  1234.   author empty$
  1235.     { format.editors "author and editor" output.check
  1236.       editor format.key output
  1237.     }
  1238.     { format.authors output.nonnull
  1239.       crossref missing$
  1240.     { "author and editor" editor either.or.check }
  1241.     'skip$
  1242.       if$
  1243.     }
  1244.   if$
  1245.   output.year.check                % special for apalike
  1246.   new.block
  1247.   format.btitle "title" output.check
  1248.   crossref missing$
  1249.     { format.bvolume output
  1250.       format.chapter.pages "chapter and pages" output.check
  1251.       new.block
  1252.       format.number.series output
  1253.       new.sentence
  1254.       publisher "publisher" output.check
  1255.       address output
  1256.     }
  1257.     { format.chapter.pages "chapter and pages" output.check
  1258.       new.block
  1259.       format.book.crossref output.nonnull
  1260.     }
  1261.   if$
  1262.   format.edition output
  1263.   new.block
  1264.   note output
  1265.   fin.entry
  1266. }
  1267. % An incollection is like inbook, but where there is a separate title
  1268. % for the referenced thing (and perhaps an editor for the whole).
  1269. % An incollection may CROSSREF a book.
  1270. %    Required: author, title, booktitle, publisher, year
  1271. %    Optional: editor, volume or number, series, type, chapter, pages,
  1272. %            address, edition, month, note
  1273. FUNCTION {incollection}
  1274. { output.bibitem
  1275.   format.authors "author" output.check
  1276.   author format.key output                % special for
  1277.   output.year.check                    % apalike
  1278.   new.block
  1279.   format.title "title" output.check
  1280.   new.block
  1281.   crossref missing$
  1282.     { format.in.ed.booktitle "booktitle" output.check
  1283.       format.bvolume output
  1284.       format.number.series output
  1285.       format.chapter.pages output
  1286.       new.sentence
  1287.       publisher "publisher" output.check
  1288.       address output
  1289.       format.edition output
  1290.     }
  1291.     { format.incoll.inproc.crossref output.nonnull
  1292.       format.chapter.pages output
  1293.     }
  1294.   if$
  1295.   new.block
  1296.   note output
  1297.   fin.entry
  1298. }
  1299. % An inproceedings is an article in a conference proceedings, and it may
  1300. % CROSSREF a proceedings.  If there's no address field, the month (& year)
  1301. % will appear just before note.
  1302. %    Required: author, title, booktitle, year
  1303. %    Optional: editor, volume or number, series, pages, address, month,
  1304. %            organization, publisher, note
  1305. FUNCTION {inproceedings}
  1306. { output.bibitem
  1307.   format.authors "author" output.check
  1308.   author format.key output                % special for
  1309.   output.year.check                    % apalike
  1310.   new.block
  1311.   format.title "title" output.check
  1312.   new.block
  1313.   crossref missing$
  1314.     { format.in.ed.booktitle "booktitle" output.check
  1315.       format.bvolume output
  1316.       format.number.series output
  1317.       format.pages output
  1318.       address output                    % for apalike
  1319.       new.sentence                    % there's no year
  1320.       organization output                % here so things
  1321.       publisher output                    % are simpler
  1322.     }
  1323.     { format.incoll.inproc.crossref output.nonnull
  1324.       format.pages output
  1325.     }
  1326.   if$
  1327.   new.block
  1328.   note output
  1329.   fin.entry
  1330. }
  1331. % The conference function is included for Scribe compatibility.
  1332. FUNCTION {conference} { inproceedings }
  1333. % A manual is technical documentation.
  1334. %    Required: title
  1335. %    Optional: author, organization, address, edition, month, year, note
  1336. FUNCTION {manual}
  1337. { output.bibitem
  1338.   format.authors output
  1339.   author format.key output                % special for
  1340.   output.year.check                    % apalike
  1341.   new.block
  1342.   format.btitle "title" output.check
  1343.   organization address new.block.checkb
  1344.   organization output
  1345.   address output
  1346.   format.edition output
  1347.   new.block
  1348.   note output
  1349.   fin.entry
  1350. }
  1351. % A mastersthesis is a Master's thesis.
  1352. %    Required: author, title, school, year
  1353. %    Optional: type, address, month, note
  1354. FUNCTION {mastersthesis}
  1355. { output.bibitem
  1356.   format.authors "author" output.check
  1357.   author format.key output                % special for
  1358.   output.year.check                    % apalike
  1359.   new.block
  1360.   format.title "title" output.check
  1361.   new.block
  1362.   "Master's thesis" format.thesis.type output.nonnull
  1363.   school "school" output.check
  1364.   address output
  1365.   new.block
  1366.   note output
  1367.   fin.entry
  1368. }
  1369. % A misc is something that doesn't fit elsewhere.
  1370. %    Required: at least one of the `optional' fields
  1371. %    Optional: author, title, howpublished, month, year, note
  1372. FUNCTION {misc}
  1373. { output.bibitem
  1374.   format.authors output
  1375.   author format.key output                % special for
  1376.   output.year.check                    % apalike
  1377.   new.block
  1378.   format.title output
  1379.   new.block
  1380.   howpublished output
  1381.   new.block
  1382.   note output
  1383.   fin.entry
  1384. }
  1385. % A phdthesis is like a mastersthesis.
  1386. %    Required: author, title, school, year
  1387. %    Optional: type, address, month, note
  1388. FUNCTION {phdthesis}
  1389. { output.bibitem
  1390.   format.authors "author" output.check
  1391.   author format.key output                % special for
  1392.   output.year.check                    % apalike
  1393.   new.block
  1394.   format.btitle "title" output.check
  1395.   new.block
  1396.   "PhD thesis" format.thesis.type output.nonnull
  1397.   school "school" output.check
  1398.   address output
  1399.   new.block
  1400.   note output
  1401.   fin.entry
  1402. }
  1403. % A proceedings is a conference proceedings.
  1404. % If there is an organization but no editor field, the organization will
  1405. % appear as the first optional field (we try to make the first block nonempty);
  1406. % if there's no address field, the month (& year) will appear just before note.
  1407. %    Required: title, year
  1408. %    Optional: editor, volume or number, series, address, month,
  1409. %            organization, publisher, note
  1410. FUNCTION {proceedings}
  1411. { output.bibitem
  1412.   format.editors output
  1413.   editor format.key output                % special for
  1414.   output.year.check                    % apalike
  1415.   new.block
  1416.   format.btitle "title" output.check
  1417.   format.bvolume output
  1418.   format.number.series output
  1419.   address output                % for apalike
  1420.   new.sentence                    % we always output
  1421.   organization output                % a nonempty organization
  1422.   publisher output                % here
  1423.   new.block
  1424.   note output
  1425.   fin.entry
  1426. }
  1427. % A techreport is a technical report.
  1428. %    Required: author, title, institution, year
  1429. %    Optional: type, number, address, month, note
  1430. FUNCTION {techreport}
  1431. { output.bibitem
  1432.   format.authors "author" output.check
  1433.   author format.key output                % special for
  1434.   output.year.check                    % apalike
  1435.   new.block
  1436.   format.title "title" output.check
  1437.   new.block
  1438.   format.tr.number output.nonnull
  1439.   institution "institution" output.check
  1440.   address output
  1441.   new.block
  1442.   note output
  1443.   fin.entry
  1444. }
  1445. % An unpublished is something that hasn't been published.
  1446. %    Required: author, title, note
  1447. %    Optional: month, year
  1448. FUNCTION {unpublished}
  1449. { output.bibitem
  1450.   format.authors "author" output.check
  1451.   author format.key output                % special for
  1452.   output.year.check                    % apalike
  1453.   new.block
  1454.   format.title "title" output.check
  1455.   new.block
  1456.   note "note" output.check
  1457.   fin.entry
  1458. }
  1459. % We use entry type `misc' for an unknown type; BibTeX gives a warning.
  1460. FUNCTION {default.type} { misc }
  1461. % Here are macros for common things that may vary from style to style.
  1462. % Users are encouraged to use these macros.
  1463. %
  1464. % Months are either written out in full or abbreviated
  1465. MACRO {jan} {"January"}
  1466. MACRO {feb} {"February"}
  1467. MACRO {mar} {"March"}
  1468. MACRO {apr} {"April"}
  1469. MACRO {may} {"May"}
  1470. MACRO {jun} {"June"}
  1471. MACRO {jul} {"July"}
  1472. MACRO {aug} {"August"}
  1473. MACRO {sep} {"September"}
  1474. MACRO {oct} {"October"}
  1475. MACRO {nov} {"November"}
  1476. MACRO {dec} {"December"}
  1477. % Journals are either written out in full or abbreviated;
  1478. % the abbreviations are like those found in ACM publications.
  1479. %
  1480. % To get a completely different set of abbreviations, it may be best to make
  1481. % a separate .bib file with nothing but those abbreviations; users could then
  1482. % include that file name as the first argument to the \bibliography command
  1483. MACRO {acmcs} {"ACM Computing Surveys"}
  1484. MACRO {acta} {"Acta Informatica"}
  1485. MACRO {cacm} {"Communications of the ACM"}
  1486. MACRO {ibmjrd} {"IBM Journal of Research and Development"}
  1487. MACRO {ibmsj} {"IBM Systems Journal"}
  1488. MACRO {ieeese} {"IEEE Transactions on Software Engineering"}
  1489. MACRO {ieeetc} {"IEEE Transactions on Computers"}
  1490. MACRO {ieeetcad}
  1491.  {"IEEE Transactions on Computer-Aided Design of Integrated Circuits"}
  1492. MACRO {ipl} {"Information Processing Letters"}
  1493. MACRO {jacm} {"Journal of the ACM"}
  1494. MACRO {jcss} {"Journal of Computer and System Sciences"}
  1495. MACRO {scp} {"Science of Computer Programming"}
  1496. MACRO {sicomp} {"SIAM Journal on Computing"}
  1497. MACRO {tocs} {"ACM Transactions on Computer Systems"}
  1498. MACRO {tods} {"ACM Transactions on Database Systems"}
  1499. MACRO {tog} {"ACM Transactions on Graphics"}
  1500. MACRO {toms} {"ACM Transactions on Mathematical Software"}
  1501. MACRO {toois} {"ACM Transactions on Office Information Systems"}
  1502. MACRO {toplas} {"ACM Transactions on Programming Languages and Systems"}
  1503. MACRO {tcs} {"Theoretical Computer Science"}
  1504. % Now we read in the .BIB entries.
  1505. READ
  1506. % The sortify function converts to lower case after purify$ing; it's
  1507. % used in sorting and in computing alphabetic labels after sorting
  1508. %
  1509. % The chop.word(w,len,s) function returns either s or, if the first len
  1510. % letters of s equals w (this comparison is done in the third line of the
  1511. % function's definition), it returns that part of s after w.
  1512. FUNCTION {sortify}
  1513. { purify$
  1514.   "l" change.case$
  1515. }
  1516. INTEGERS { len }
  1517. FUNCTION {chop.word}
  1518. { 's :=
  1519.   'len :=
  1520.   s #1 len substring$ =
  1521.     { s len #1 + global.max$ substring$ }
  1522.     's
  1523.   if$
  1524. }
  1525. % This long comment applies only to alphabetic labels
  1526. %
  1527. % The format.lab.names function makes a short label by using the initials of
  1528. % the von and Last parts of the names (but if there are more than four names,
  1529. % (i.e., people) it truncates after three and adds a superscripted "+";
  1530. % it also adds such a "+" if the last of multiple authors is "others").
  1531. % If there is only one name, and its von and Last parts combined have just
  1532. % a single name-token ("Knuth" has a single token, "Brinch Hansen" has two),
  1533. % we take the first three letters of the last name.  The boolean
  1534. % et.al.char.used tells whether we've used a superscripted "+", so that we
  1535. % know whether to include a LaTeX macro for it.
  1536. %
  1537. % format.lab.names(s) ==
  1538. %  BEGIN
  1539. %    numnames := num.names$(s)
  1540. %    if numnames > 1 then
  1541. %        if numnames > 4 then
  1542. %        namesleft := 3
  1543. %        else
  1544. %        namesleft := numnames
  1545. %        nameptr := 1
  1546. %        nameresult := ""
  1547. %        while namesleft > 0
  1548. %          do
  1549. %        if (name_ptr = numnames) and
  1550. %             format.name$(s, nameptr, "{ff }{vv }{ll}{ jj}") = "others"
  1551. %           then nameresult := nameresult * "{\etalchar{+}}"
  1552. %            et.al.char.used := true
  1553. %           else nameresult := nameresult *
  1554. %                format.name$(s, nameptr, "{v{}}{l{}}")
  1555. %        nameptr := nameptr + 1
  1556. %        namesleft := namesleft - 1
  1557. %          od
  1558. %        if numnames > 4 then
  1559. %        nameresult := nameresult * "{\etalchar{+}}"
  1560. %        et.al.char.used := true
  1561. %    else
  1562. %        t := format.name$(s, 1, "{v{}}{l{}}")
  1563. %        if text.length$(t) < 2 then    % there's just one name-token
  1564. %        nameresult := text.prefix$(format.name$(s,1,"{ll}"),3)
  1565. %        else
  1566. %        nameresult := t
  1567. %        fi
  1568. %    fi
  1569. %    return nameresult
  1570. %  END
  1571. %
  1572. % Exactly what fields we look at in constructing the primary part of the label
  1573. % depends on the entry type; this selectivity (as opposed to, say, always
  1574. % looking at author, then editor, then key) helps ensure that "ignored" fields,
  1575. % as described in the LaTeX book, really are ignored.  Note that MISC is part
  1576. % of the deepest `else' clause in the nested part of calc.label; thus, any
  1577. % unrecognized entry type in the database is handled correctly.
  1578. %
  1579. % There is one auxiliary function for each of the four different sequences of
  1580. % fields we use.  The first of these functions looks at the author field, and
  1581. % then, if necessary, the key field.  The other three functions, which might
  1582. % look at two fields and the key field, are similar, except that the key field
  1583. % takes precedence over the organization field (for labels---not for sorting).
  1584. %
  1585. % The calc.label function calculates the preliminary label of an entry, which
  1586. % is formed by taking three letters of information from the author or editor or
  1587. % key or organization field (depending on the entry type and on what's empty,
  1588. % but ignoring a leading "The " in the organization), and appending the last
  1589. % two characters (digits) of the year. It is an error if the appropriate fields
  1590. % among author, editor, organization, and key are missing, and we use
  1591. % the first three letters of the cite$ in desperation when this happens.
  1592. % The resulting label has the year part, but not the name part, purify$ed
  1593. % (purify$ing the year allows some sorting shenanigans by the user).
  1594. %
  1595. % This function also calculates the version of the label to be used in sorting.
  1596. %
  1597. % The final label may need a trailing 'a', 'b', etc., to distinguish it from
  1598. % otherwise identical labels, but we can't calculated those "extra.label"s
  1599. % until after sorting.
  1600. %
  1601. % calc.label ==
  1602. %  BEGIN
  1603. %    if type$ = "book" or "inbook" then
  1604. %        author.editor.key.label
  1605. %    else if type$ = "proceedings" then
  1606. %        editor.key.organization.label
  1607. %    else if type$ = "manual" then
  1608. %        author.key.organization.label
  1609. %    else
  1610. %        author.key.label
  1611. %    fi fi fi
  1612. %    label := label * substring$(purify$(field.or.null(year)), -1, 2)
  1613. %        % assuming we will also sort, we calculate a sort.label
  1614. %    sort.label := sortify(label), but use the last four, not two, digits
  1615. %  END
  1616. %            There are three apalike cases: one person (Jones),
  1617. %            two (Jones and de~Bruijn), and more (Jones et~al.).
  1618. %            This function is much like format.crossref.editors.
  1619. %
  1620. FUNCTION {format.lab.names}
  1621. { 's :=
  1622.   s #1 "{vv~}{ll}" format.name$
  1623.   s num.names$ duplicate$
  1624.   #2 >
  1625.     { pop$ " et~al." * }
  1626.     { #2 <
  1627.     'skip$
  1628.     { s #2 "{ff }{vv }{ll}{ jj}" format.name$ "others" =
  1629.         { " et~al." * }
  1630.         { " and " * s #2 "{vv~}{ll}" format.name$ * }
  1631.       if$
  1632.     }
  1633.       if$
  1634.     }
  1635.   if$
  1636. }
  1637. FUNCTION {author.key.label}
  1638. { author empty$
  1639.     { key empty$
  1640.     { cite$ #1 #3 substring$ }
  1641.     'key                    % apalike uses the whole key
  1642.       if$
  1643.     }
  1644.     { author format.lab.names }
  1645.   if$
  1646. }
  1647. FUNCTION {author.editor.key.label}
  1648. { author empty$
  1649.     { editor empty$
  1650.     { key empty$
  1651.         { cite$ #1 #3 substring$ }
  1652.         'key                % apalike uses the whole key
  1653.       if$
  1654.     }
  1655.     { editor format.lab.names }
  1656.       if$
  1657.     }
  1658.     { author format.lab.names }
  1659.   if$
  1660. }
  1661. FUNCTION {editor.key.label}
  1662. { editor empty$
  1663.     { key empty$
  1664.     { cite$ #1 #3 substring$ }
  1665.     'key            % apalike uses the whole key, no organization
  1666.       if$
  1667.     }
  1668.     { editor format.lab.names }
  1669.   if$
  1670. }
  1671. FUNCTION {calc.label}
  1672. { type$ "book" =
  1673.   type$ "inbook" =
  1674.   or
  1675.     'author.editor.key.label
  1676.     { type$ "proceedings" =
  1677.     'editor.key.label            % apalike ignores organization
  1678.     'author.key.label            % for labeling and sorting
  1679.       if$
  1680.     }
  1681.   if$
  1682.   ", "                            % these three lines are
  1683.   *                            % for apalike, which
  1684.   year field.or.null purify$ #-1 #4 substring$        % uses all four digits
  1685.   *
  1686.   'label :=
  1687. }
  1688. % It doesn't seem like a particularly good idea to use an order-of-citation
  1689. % reference list when using alphabetic labels, but we need to have a
  1690. % special pass to calculate labels when this happens.
  1691. % When sorting, we compute the sortkey by executing "presort" on each entry.
  1692. % The presort key contains a number of "sortify"ed strings, concatenated
  1693. % with multiple blanks between them.  This makes things like "brinch  per"
  1694. % come before "brinch hansen  per".
  1695. %
  1696. % The fields used here are: the sort.label for alphabetic labels (as set by
  1697. % calc.label), followed by the author names (or editor names or organization
  1698. % (with a leading "The " removed) or key field, depending on entry type and on
  1699. % what's empty), followed by year, followed by the first bit of the title
  1700. % (chopping off a leading "The ", "A ", or "An ").
  1701. % Names are formatted: Von Last First Junior.
  1702. % The names within a part will be separated by a single blank
  1703. % (such as "brinch hansen"), two will separate the name parts themselves
  1704. % (except the von and last), three will separate the names,
  1705. % four will separate the names from year (and from label, if alphabetic),
  1706. % and four will separate year from title.
  1707. %
  1708. % The sort.format.names function takes an argument that should be in
  1709. % BibTeX name format, and returns a string containing "   "-separated
  1710. % names in the format described above.  The function is almost the same
  1711. % as format.names.
  1712. FUNCTION {sort.format.names}
  1713. { 's :=
  1714.   #1 'nameptr :=
  1715.   ""
  1716.   s num.names$ 'numnames :=
  1717.   numnames 'namesleft :=
  1718.     { namesleft #0 > }
  1719.     { nameptr #1 >
  1720.     { "   " * }
  1721.     'skip$
  1722.       if$                        % apalike uses initials
  1723.       s nameptr "{vv{ } }{ll{ }}{  f{ }}{  jj{ }}" format.name$ 't := % <= here
  1724.       nameptr numnames = t "others" = and
  1725.     { "et al" * }
  1726.     { t sortify * }
  1727.       if$
  1728.       nameptr #1 + 'nameptr :=
  1729.       namesleft #1 - 'namesleft :=
  1730.     }
  1731.   while$
  1732. }
  1733. % The sort.format.title function returns the argument,
  1734. % but first any leading "A "'s, "An "'s, or "The "'s are removed.
  1735. % The chop.word function uses s, so we need another string variable, t
  1736. FUNCTION {sort.format.title}
  1737. { 't :=
  1738.   "A " #2
  1739.     "An " #3
  1740.       "The " #4 t chop.word
  1741.     chop.word
  1742.   chop.word
  1743.   sortify
  1744.   #1 global.max$ substring$
  1745. }
  1746. % The auxiliary functions here, for the presort function, are analogous to
  1747. % the ones for calc.label; the same comments apply, except that the
  1748. % organization field takes precedence here over the key field.  For sorting
  1749. % purposes, we still remove a leading "The " from the organization field.
  1750. FUNCTION {author.sort}
  1751. { author empty$
  1752.     { key empty$
  1753.     { "to sort, need author or key in " cite$ * warning$
  1754.       ""
  1755.     }
  1756.     { key sortify }
  1757.       if$
  1758.     }
  1759.     { author sort.format.names }
  1760.   if$
  1761. }
  1762. FUNCTION {author.editor.sort}
  1763. { author empty$
  1764.     { editor empty$
  1765.     { key empty$
  1766.         { "to sort, need author, editor, or key in " cite$ * warning$
  1767.           ""
  1768.         }
  1769.         { key sortify }
  1770.       if$
  1771.     }
  1772.     { editor sort.format.names }
  1773.       if$
  1774.     }
  1775.     { author sort.format.names }
  1776.   if$
  1777. }
  1778. FUNCTION {editor.sort}
  1779. { editor empty$
  1780.     { key empty$
  1781.     { "to sort, need editor or key in " cite$ * warning$
  1782.       ""
  1783.     }
  1784.     { key sortify }
  1785.       if$
  1786.     }
  1787.     { editor sort.format.names }
  1788.   if$
  1789. }
  1790. %            apalike uses two sorting passes; the first one sets the
  1791. %            labels so that the `a's, `b's, etc. can be computed;
  1792. %            the second pass puts the references in "correct" order.
  1793. %            The presort function is for the first pass. It computes
  1794. %            label, sort.label, and title, and then concatenates.
  1795. FUNCTION {presort}
  1796. { calc.label
  1797.   label sortify
  1798.   "    "
  1799.   *
  1800.   type$ "book" =
  1801.   type$ "inbook" =
  1802.   or
  1803.     'author.editor.sort
  1804.     { type$ "proceedings" =
  1805.     'editor.sort
  1806.     'author.sort
  1807.       if$
  1808.     }
  1809.   if$
  1810.   #1 entry.max$ substring$    % for
  1811.   'sort.label :=        % apalike
  1812.   sort.label            % style
  1813.   *
  1814.   "    "
  1815.   *
  1816.   title field.or.null
  1817.   sort.format.title
  1818.   *
  1819.   #1 entry.max$ substring$
  1820.   'sort.key$ :=
  1821. }
  1822. ITERATE {presort}
  1823. % And now we can sort
  1824. SORT        % by label, sort.label, title---for final label calculation
  1825. % This long comment applies only to alphabetic labels, when sorted
  1826. %
  1827. % Now comes the final computation for alphabetic labels, putting in the 'a's
  1828. % and 'b's and so forth if required.  This involves two passes: a forward
  1829. % pass to put in the 'b's, 'c's and so on, and a backwards pass
  1830. % to put in the 'a's (we don't want to put in 'a's unless we know there
  1831. % are 'b's).
  1832. % We have to keep track of the longest (in width$ terms) label, for use
  1833. % by the "thebibliography" environment.
  1834. %
  1835. % VAR: longest.label, last.sort.label, next.extra: string
  1836. %      longest.label.width, last.extra.num: integer
  1837. %
  1838. % initialize.longest.label ==
  1839. %  BEGIN
  1840. %    longest.label := ""
  1841. %    last.sort.label := int.to.chr$(0)
  1842. %    next.extra := ""
  1843. %    longest.label.width := 0
  1844. %    last.extra.num := 0
  1845. %  END
  1846. %
  1847. % forward.pass ==
  1848. %  BEGIN
  1849. %    if last.sort.label = sort.label then
  1850. %        last.extra.num := last.extra.num + 1
  1851. %        extra.label := int.to.chr$(last.extra.num)
  1852. %    else
  1853. %        last.extra.num := chr.to.int$("a")
  1854. %        extra.label := ""
  1855. %        last.sort.label := sort.label
  1856. %    fi
  1857. %  END
  1858. %
  1859. % reverse.pass ==
  1860. %  BEGIN
  1861. %    if next.extra = "b" then
  1862. %        extra.label := "a"
  1863. %    fi
  1864. %    label := label * extra.label
  1865. %    if width$(label) > longest.label.width then
  1866. %        longest.label := label
  1867. %        longest.label.width := width$(label)
  1868. %    fi
  1869. %    next.extra := extra.label
  1870. %  END
  1871. STRINGS { last.label next.extra }    % apalike labels are only for the text;
  1872. INTEGERS { last.extra.num }        % there are none in the bibliography
  1873. FUNCTION {initialize.extra.label.stuff}    % and hence there is no `longest.label'
  1874. { #0 int.to.chr$ 'last.label :=
  1875.   "" 'next.extra :=
  1876.   #0 'last.extra.num :=
  1877. }
  1878. FUNCTION {forward.pass}
  1879. { last.label label =
  1880.     { last.extra.num #1 + 'last.extra.num :=
  1881.       last.extra.num int.to.chr$ 'extra.label :=
  1882.     }
  1883.     { "a" chr.to.int$ 'last.extra.num :=
  1884.       "" 'extra.label :=
  1885.       label 'last.label :=
  1886.     }
  1887.   if$
  1888. }
  1889. FUNCTION {reverse.pass}
  1890. { next.extra "b" =
  1891.     { "a" 'extra.label := }
  1892.     'skip$
  1893.   if$
  1894.   label extra.label * 'label :=
  1895.   extra.label 'next.extra :=
  1896. }
  1897. EXECUTE {initialize.extra.label.stuff}
  1898. ITERATE {forward.pass}
  1899. REVERSE {reverse.pass}
  1900. %                Now that the label is right we sort for real,
  1901. %                on sort.label then year then title.  This is
  1902. %                for the second sorting pass.
  1903. FUNCTION {bib.sort.order}
  1904. { sort.label
  1905.   "    "
  1906.   *
  1907.   year field.or.null sortify
  1908.   *
  1909.   "    "
  1910.   *
  1911.   title field.or.null
  1912.   sort.format.title
  1913.   *
  1914.   #1 entry.max$ substring$
  1915.   'sort.key$ :=
  1916. }
  1917. ITERATE {bib.sort.order}
  1918. SORT        % by sort.label, year, title---giving final bibliography order
  1919. % Now we're ready to start writing the .BBL file.
  1920. % We begin, if necessary, with a LaTeX macro for unnamed names in an alphabetic
  1921. % label; next comes stuff from the `preamble' command in the database files.
  1922. % Then we give an incantation containing the command
  1923. %     \begin{thebibliography}{...}
  1924. % where the `...' is the longest label.
  1925. %
  1926. % We also call init.state.consts, for use by the output routines.
  1927. FUNCTION {begin.bib}
  1928. { preamble$ empty$                % no \etalchar in apalike
  1929.     'skip$
  1930.     { preamble$ write$ newline$ }
  1931.   if$
  1932.   "\begin{thebibliography}{}" write$ newline$        % no labels in apalike
  1933. }
  1934. EXECUTE {begin.bib}
  1935. EXECUTE {init.state.consts}
  1936. % Now we produce the output for all the entries
  1937. ITERATE {call.type$}
  1938. % Finally, we finish up by writing the `\end{thebibliography}' command.
  1939. FUNCTION {end.bib}
  1940. { newline$
  1941.   "\end{thebibliography}" write$ newline$
  1942. }
  1943. EXECUTE {end.bib}
  1944.